import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import mean_squared_error
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
import matplotlib.pyplot as plt
import seaborn as sns
from imblearn.over_sampling import RandomOverSampler
from collections import Counter
import numpy as np

# 读取CSV文件
data = pd.read_csv('./data/情感文本分析/sentimentdataset.csv')

# 文本预处理（仅保留文本列，并简单提取摘要）
text_data = data['Text']  # 假设文本列名为'Text'
sentiment_labels = data['Sentiment']  # 假设情感分类列名为'Sentiment'
summaries = text_data.str.split('.').str[:3].apply(lambda x: '. '.join(x) + '.')  # 提取前三句作为摘要，并添加句点以保持语法正确

# 词汇表示：使用TF-IDF向量化器（这里可以添加更多参数进行优化，如n-gram范围、停用词等）
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(text_data)

# 划分训练集和测试集（这里使用更大的测试集以更快地进行评估）
X_train, X_test, y_train, y_test = train_test_split(X, sentiment_labels, test_size=0.3, random_state=42)

# 文本分类：使用线性SVM分类器（通常比朴素贝叶斯更快，尤其是在大数据集上）
clf = LinearSVC(random_state=42, dual=False)  # dual=False可以提高在大数据集上的性能
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)


# 输出分类报告
print(classification_report(y_test, y_pred))

y_test_class = (y_test > y_pred).astype(int)
table_data = [
    ["Model", "Accuracy", "Recall", "Precision", "F1 Score"],
    ['SVR', f"{accuracy_score(y_test_class, y_test_class):.2f}",
     f"{recall_score(y_test_class, y_test_class):.2f}",
     f"{precision_score(y_test_class, y_test_class):.2f}",
     f"{f1_score(y_test_class, y_test_class):.2f}"]
]

fig, ax = plt.subplots(figsize=(10, 7))
ax.axis('tight')
ax.axis('off')
the_table = ax.table(cellText=table_data, loc='center', cellLoc='center')
the_table.auto_set_font_size(False)
the_table.set_fontsize(14)
the_table.auto_set_column_width(
    col=list(range(len(["Model", "Accuracy", "Recall", "Precision", "F1 Score"]))))

# 设置表格的列宽和行高为自适应
for (i, j), cell in the_table.get_celld().items():
    cell.set_text_props(fontproperties=plt.matplotlib.font_manager.FontProperties(weight='bold'))
plt.legend()
plt.show()
# plt.savefig('Sales4.png')  # 保存图像

# 复杂的可视化图表展示结果（与之前相同）
cm = pd.crosstab(y_test, y_pred, rownames=['Actual'], colnames=['Predicted'], margins=True)
sns.heatmap(cm, annot=True, cmap='coolwarm', fmt='d')
plt.title('Sentiment Classification Confusion Matrix')
plt.xlabel('Predicted Sentiment')
plt.ylabel('Actual Sentiment')
plt.legend()
plt.show()